<template>
	<view>
		<u-modal v-model="show" :show-title="false" :async-close="true" :show-confirm-button="false" :mask-close-able="true">
			<!-- #ifndef MP-WEIXIN -->
			<u-tabs :list="filter.tabs(['账号密码', '手机短信'])" :is-scroll="false" :current="formData.type" @change="i=>formData.type=i" active-color="#00b280" />
			<u-form class="mt2 px4" label-position="left" :model="view" ref="uForm">
				<u-form-item label="手机号:" prop="mobile" label-width="140">
					<u-input v-model:value="formData.mobile" placeholder="请输入手机号" maxlength="11" type="number" />
					<!-- #ifdef MP -->
					<u-button slot="right" size="mini" ref="getMobile" open-type="getPhoneNumber" @getphonenumber="bind">本机号</u-button>
					<!-- #endif -->
				</u-form-item>
				<u-form-item v-if="formData.type==0" label="密码:" prop="password" label-width="140" :label-bottom="false">
					<u-input v-model:value="formData.password" placeholder="请输入密码" maxlength="16" type="password" />
				</u-form-item>
				<u-form-item v-else label="验证码:" prop="smscode" label-width="140" :label-bottom="false">
					<u-input v-model:value="formData.smscode" placeholder="请输入短信验证码" maxlength="4" type="number" />
					<text slot="right" class="tag p2" v-if="!sended">发送</text>
					<text slot="right" class="tag-light p2" v-else>{{seconds}}</text>
				</u-form-item>
			</u-form>
			<view class="fjb py8 px2 bt">
				<navigator url="/pages/index/public/signup" class="fc-green">注册</navigator>
				<navigator url="/pages/index/public/forget" class="c6">忘记密码?</navigator>
			</view>
			<!--<u-button v-if="formData.type===1&&!sended" @click="getcode" type="success">获取手机验证码</u-button>-->
			<button type="primary" @click="submit" :disabled="handing" :loading="handing">登录</button>
			<!-- #ifdef APP-PLUS -->
			<view class="fjc p4">
				<button size="mini" type="info" ref="getMobile" open-type="getPhoneNumber" @getphonenumber="bind">本机号一键绑定</button>
			</view>
			<!-- #endif -->
			<!-- #endif -->
			<!-- #ifdef MP-WEIXIN -->
			<view class="bgw px6">
				<view class="tac py6 fs26">利示共享零售</view>
				<view class="h30 fjc fac">
					<image :lazy-load="true" class="w14 h14 br3" src="/static/logo.png" />
				</view>
				<view class="tac">
					<button size="mini" ref="getMobile" type="primary" open-type="getPhoneNumber" @getphonenumber="bind">确定</button>
				</view>
			</view>
			<!-- #endif -->
		</u-modal>
	</view>
</template>
<script lang="ts">
import {page,form} from '@/mixins'
import {md5} from "crypto-js"

const ruleList = [{
		mobile: [{
			type: 'number',
			required: true,
			len: 11,
			message: '请输入正确的手机号',
			trigger: ['blur']
		}],
		password: [{
			type: 'string',
			required: true,
			min: 6, max: 16,
			message: '请输入密码,6-16位字符',
			trigger: ['blur']
		}]
	}, {
		mobile: [{
			type: 'number',
			required: true,
			message: '请输入正确的手机号',
			len: 11,
			trigger: ['blur']
		}],
		smscode: [{
			type: 'string',
			required: true,
			len: 4,
			message: '请输入4位短信验证码',
			trigger: ['blur']
		}]
	}
]

export default {
	mixins: [page,form],
	data() {
		return {
			view: { type: 0, mobile:'', password:'', smscode: '' },
			sended: false,
			seconds: 120,
			formPath: 'login'
		}
	},
	computed: {
		show: {
			get(){
				return uni.$store.main.showAuth && !this.auth
			},
			set(value){
				uni.$store.main.update({showAuth: value})
			}
		},
		rules(){
			return ruleList[this.formData.type]
		}
	},
	methods: {
		getcode(){
			if(!this.formData.mobile){
				return this.$u.toast('请输入的手机号')
			}
			else if(!this.$u.test.mobile(this.formData.mobile)){
				return this.$u.toast('请输入正确的手机号')
			}
			//this.$u.post('index/sms/auth', {mobile:this.formData.mobile}).then(res => {
			this.sended=true
			const interval = setInterval(()=>{
				this.seconds -= 1
				if(this.seconds <= 0){
					this.sended = false
					clearInterval(interval)
				}
			}, 1000)
			this.seconds=60
			//}).catch(()=>{})
		},
		finish(e){
			this.formData.smscode=e
			this.submit()
		},
		save() {
			this.handing = true
			this.$u.post(this.formPath, {
				verifyId: this.verifyId,
				...this.view,
				password: md5(this.formData.password)
			}).then(res => {
				this.sended=false
				uni.$store.user.login(res)
				uni.$store.main.update({showAuth: false})
				this.handing = false
				// uni.$emit('handleLogin',{})
				/*
				this.$tim.login({userID: 'user' + res.user.id, userSig: res.user.timsig}).then(resp=>{
					if (resp.data.repeatLogin === true) {
						// 标识账号已登录，本次登录操作为重复登录。v2.5.1 起支持
						// console.log(resp.data.errorInfo);
					}
				}).catch(error=>{
					// console.warn('login error:', error); // 登录失败的相关信息
				})
				*/
			}).catch(res=>{
				this.handing = false
				this.formData.smscode=''
			})
		},
		bind(e) {
			// #ifdef MP-WEIXIN
			if (e.detail.errMsg == 'getPhoneNumber:fail user deny') {
				// console.log('用户拒绝提供手机号');
			} else {
				this.$u.post('index/wemini/bind', {...e.detail}).then(resp => {
					this.$store.user.login(resp)
					this.$store.showAuth = false
					// util.back('index')
				}).catch(()=>{})
			}
			// #endif
		}
	}
}
</script>
